package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.bo.LogiScheduleFeeEditBo;
import com.piggy.logi.sys.bo.LogiScheduleFeeQueryBo;
import com.piggy.logi.sys.domain.LogiScheduleFee;
import com.piggy.logi.sys.mapper.LogiScheduleFeeMapper;
import com.piggy.logi.sys.service.ILogiScheduleFeeService;
import com.piggy.logi.sys.vo.LogiScheduleFeeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 调度线路分段费用Service业务层处理
 *
 * @author piggy
 * @date 2023-09-13
 */
@Slf4j
@Service
public class LogiScheduleFeeServiceImpl extends ServiceImpl<LogiScheduleFeeMapper, LogiScheduleFee> implements ILogiScheduleFeeService {

    @Override
    public LogiScheduleFeeVo queryById(Long id) {
        return getVoById(id, LogiScheduleFeeVo.class);
    }

    @Override
    public TableDataInfo<LogiScheduleFeeVo> queryPageList(LogiScheduleFeeQueryBo bo) {
        PagePlus<LogiScheduleFee, LogiScheduleFeeVo> result = pageVo(PageUtils.buildPagePlus(bo, true), buildQueryWrapper(bo), LogiScheduleFeeVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiScheduleFeeVo> queryList(LogiScheduleFeeQueryBo bo) {
        return listVo(buildQueryWrapper(bo), LogiScheduleFeeVo.class);
    }

    private LambdaQueryWrapper<LogiScheduleFee> buildQueryWrapper(LogiScheduleFeeQueryBo bo) {
        LambdaQueryWrapper<LogiScheduleFee> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, LogiScheduleFee::getId, bo.getId());
        lqw.eq(bo.getScheduleId() != null, LogiScheduleFee::getScheduleId, bo.getScheduleId());
        lqw.eq(bo.getLineId() != null, LogiScheduleFee::getLineId, bo.getLineId());
        lqw.eq(StrUtil.isNotBlank(bo.getFeeMode()), LogiScheduleFee::getFeeMode, bo.getFeeMode());
        lqw.eq(bo.getDepDeptId() != null, LogiScheduleFee::getDepDeptId, bo.getDepDeptId());
        lqw.eq(bo.getDesDeptId() != null, LogiScheduleFee::getDesDeptId, bo.getDesDeptId());
        lqw.eq(bo.getRefund() != null, LogiScheduleFee::getRefund, bo.getRefund());
        lqw.eq(bo.getCod() != null, LogiScheduleFee::getCod, bo.getCod());
        lqw.eq(bo.getPod() != null, LogiScheduleFee::getPod, bo.getPod());
        lqw.eq(bo.getPoa() != null, LogiScheduleFee::getPoa, bo.getPoa());
        lqw.eq(bo.getPom() != null, LogiScheduleFee::getPom, bo.getPom());
        lqw.eq(bo.getMs() != null, LogiScheduleFee::getMs, bo.getMs());
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiScheduleFeeVo insertByAddBo(LogiScheduleFeeEditBo bo) {
        LogiScheduleFee add = BeanUtil.toBean(bo, LogiScheduleFee.class);
        save(add);
        return queryById(add.getId());
    }

    @Override
    @Transactional
    public LogiScheduleFeeVo updateByEditBo(LogiScheduleFeeEditBo bo) {
        LogiScheduleFee update = BeanUtil.toBean(bo, LogiScheduleFee.class);
        updateById(update);
        return queryById(update.getId());
    }

    @Override
    @Transactional
    public Boolean batchByAddBo(List<LogiScheduleFeeEditBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return false;
        }

        List<LogiScheduleFee> bookList = BeanUtil.copyToList(boList, LogiScheduleFee.class);
        return this.saveBatch(bookList);
    }

    @Override
    @Transactional
    public Boolean batchByUpdateBo(List<LogiScheduleFeeEditBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return false;
        }

        this.remove(Wrappers.<LogiScheduleFee>lambdaQuery().eq(LogiScheduleFee::getScheduleId, boList.get(0).getScheduleId()));
        return this.saveBatch(BeanUtil.copyToList(boList, LogiScheduleFee.class));
    }

    @Override
    @Transactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
